import { getUser,getuserToAsc,getuserToDesc,deleteUser,changePassword,changePower } from '../services/getuser';
import { register } from '../services/login';
import {
  notification,Icon
} from 'antd';
export default {

    namespace: 'user',
  
    state: {
      addUser:{
        username:"",
        password:"",
        isvip:0,
        addUservisible:false,
        isCheckd:true
      },
  
        filter: "",
        current: 1, 
        deletevisible: false,
        ChangePasswordvisible: false,
        editWindow: false,
        Usersdata: [],
     
        userName: "",
        total: 1,
        powervisible: false,
        validColumns: ['insertdate','userName','isvip', 'action'],
        loading: true,
        operationUser:{
          userId: "",
          userName: "",
          vip:0
        },
    
     
    },
  
    subscriptions: {
      setup({ dispatch, history }) {
          // eslint-disable-line
     
        if (history.location.pathname.match(/\/user/i)) {
            dispatch({
              type: "getuser",
              payload: {
                filter:""
              }
            })
    
          }
      },
    },
  
    effects: {
      *fetch({ payload }, { call, put }) {  // eslint-disable-line
       
      },
      *getuser({ payload }, { call, put }) {  // eslint-disable-line
        const result = yield call(getUser, payload); 
        console.log(result)

        if(result.data){
            yield put({
                type:'setState',
                payload:{
                   Usersdata: result.data.users,
                   loading:false,
                 
                }
              })
        }
      },
      *userToAsc({ payload }, { call, put }) {  // eslint-disable-line
        const result = yield call(getuserToAsc, payload); 
        console.log(result)
        if(result.data){
          yield put({
              type:'setState',
              payload:{
                 Usersdata: result.data.users,
                 loading:false
              }
            })
      }
      },
      *userToDesc({ payload }, { call, put }) {  // eslint-disable-line
        const result = yield call(getuserToDesc, payload); 
        console.log(result)
        if(result.data){
          yield put({
              type:'setState',
              payload:{
                 Usersdata: result.data.users,
                 loading:false
              }
            })
      }
      },
      
    * delete({ payload}, {call,put,select}) {
      let userName = yield select((state) => state.user.operationUser.userName)
     
       const result = yield call(deleteUser, payload);
      console.log(result)
      
      yield put({
        type: 'user/setState',
        payload: {
          deletevisible: false,
        }
      })
      if(result.data.delete){
        notification.open({
          message: '删除成功',
          description: '您已成功删除用户'+ userName,
          icon: <Icon type="smile" style={{ color: '#108ee9' }} />,
        });
      }
      
      yield put({
        type: 'getuser',
        payload: {
        
        }
      })
   
    },
    *register({ payload }, { call, put }) { 
      const result = yield call(register, payload); 
      console.log(result) 
      if(result.data.isreg){
        notification.open({
          message: '注册成功',
          description: '您已成功注册用户',
          icon: <Icon type="smile" style={{ color: 'green' }} />,
        });
        yield put({
          type:'setState',
          payload:{
            addUser:{
              addUservisible:false
            }
          }
        })
      }else {
        notification.open({
          message: '注册失败',
          description: '用户名已存在',
          icon: <Icon type="smile" style={{ color: 'red' }} />,
        });
      }
      yield put({
        type: 'getuser',
        payload: {
        
        }
      })
    },
    *ChangeUserPassword({ payload }, { call, put,select }) { 
      let userId = yield select((state) => state.user.operationUser.userId)
      payload.id=userId
      const result = yield call(changePassword, payload); 
      console.log(result) 
      if(result.err){
        yield put({
          type:'setState',
          payload:{
              ChangePasswordvisible: false,
          }
        })
        notification.open({
          message: '修改失败',
          description: '修改用户密码失败',
          icon: <Icon type="smile" style={{ color: 'red' }} />,
        });
      }

      if(result.data.changePass){
        yield put({
          type:'setState',
          payload:{
              ChangePasswordvisible: false,
          }
        })
        notification.open({
          message: '修改成功',
          description: '您已成功修改用户密码',
          icon: <Icon type="smile" style={{ color: 'green' }} />,
        });
      } 
    },
    *changeUserPower({ payload }, { call, put,select }) {  // eslint-disable-line
      let userId = yield select((state) => state.user.operationUser.userId)
      payload.id=userId
      const result = yield call(changePower, payload); 
      console.log(result) 
      if(result.err){
        yield put({
          type:'setState',
          payload:{
            powervisible: false,
          }
        })
        notification.open({
          message: '修改失败',
          description: '修改用户权限失败',
          icon: <Icon type="smile" style={{ color: 'red' }} />,
        });
      }

      if(result.data.changePass){
        yield put({
          type:'setState',
          payload:{
            powervisible: false,
          }
        })
        notification.open({
          message: '修改成功',
          description: '您已成功修改用户权限',
          icon: <Icon type="smile" style={{ color: 'green' }} />,
        });
      } 
      yield put({
        type: 'getuser',
        payload: {
        
        }
      })
    },
    
    },
  
    reducers: {
        setState(state, action) {
        return { ...state, ...action.payload };
      },
    },
  
  };
  